﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Forms;

namespace Sai.UI
{
    public partial class AssemblySelection : UserControl 
    {
        public event EventHandler ListChanged;

        public AssemblySelection()
        {
            InitializeComponent();

            this.lvAssemblies.View = View.Details;

            this.lvAssemblies.Columns.Add("Name", 250, HorizontalAlignment.Left);
        }

        public ReadOnlyCollection<string> SelectedAssemblies
        {
            get
            {
                List<string> files = new List<string>();

                foreach (var item in lvAssemblies.Items)
                {
                    AssemblyListViewItem alvi = item as AssemblyListViewItem;

                    if (alvi != null)
                    {
                        files.Add(alvi.FullPath);
                    }
                }

                return new ReadOnlyCollection<string>(files);
            }
        }

        public void Initialise(IEnumerable<string> files)
        {
            //lvAssemblies.Clear();
            lvAssemblies.BeginUpdate();
            lvAssemblies.Items.Clear();

            // duplicates ???
            foreach (var file in files)
            {
                lvAssemblies.Items.Add(new AssemblyListViewItem(file));
            }

            lvAssemblies.EndUpdate();

            btnRemove.Enabled = false;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                if (!string.IsNullOrEmpty(this.Path))
                {
                    dialog.SelectedPath = this.Path;
                }
                else
                {
                    dialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                }

                if (DialogResult.OK == dialog.ShowDialog())
                {
                    this.Path = dialog.SelectedPath;

                    lvAssemblies.BeginUpdate();

                    foreach (var file in Directory.GetFiles(this.Path, "*.dll"))
                    {
                        lvAssemblies.Items.Add(new AssemblyListViewItem(file));
                    }

                    lvAssemblies.EndUpdate();

                    if (this.ListChanged != null)
                    {
                        this.ListChanged(this, EventArgs.Empty);
                    }
                }
            }
        }

        private string Path
        {
            get;
            set;
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Multiselect = true;
                dialog.Filter = "Assembly Files (*.dll)|*.dll|All Files (*.*)|*.*";

                if (DialogResult.OK == dialog.ShowDialog())
                {
                    lvAssemblies.BeginUpdate();

                    // duplicates ???
                    foreach (var file in dialog.FileNames)
                    {
                        lvAssemblies.Items.Add(new AssemblyListViewItem(file));
                    }

                    lvAssemblies.EndUpdate();

                    if (this.ListChanged != null)
                    {
                        this.ListChanged(this, EventArgs.Empty);
                    }
                }
            }
        }

        private void bntRemove_Click(object sender, EventArgs e)
        {
            if (lvAssemblies.SelectedItems.Count > 0)
            {
                lvAssemblies.BeginUpdate();

                foreach (ListViewItem item in lvAssemblies.SelectedItems)
                {
                    item.Remove();
                }

                lvAssemblies.EndUpdate();

                if (this.ListChanged != null)
                {
                    this.ListChanged(this, EventArgs.Empty);
                }
            }
        }

        private void lvAssemblies_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemove.Enabled = lvAssemblies.SelectedItems.Count > 0;
        }
       
    }
}
